// Decompiled by DJ v2.3.3.38 Copyright 2000 Atanas Neshkov  Date: 03/11/2007 6:40:39
// Home Page : http//members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   SgdbDao.java

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.SgbdDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

public class SgdbDao extends DAO {

    public SgdbDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, DRIVER, URL, PUERTO FROM SGBD WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return getDatos(resul, new SgbdDTO());
            }
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return null;
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, DRIVER, URL, PUERTO FROM SGBD");
            resul = prep.executeQuery();
            for (; resul.next(); c.add(getDatos(resul, new SgbdDTO()))) {
                ;
            }

            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex, "No se pudo Cargar el Drive");
        } catch (SQLException ex) {
            throw new IGFormatoException(ex,"No se pudo cargar la lista de Gestores de Bases de Datos");
        } catch (IGFormatoException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        SgbdDTO sgbdDTO = (SgbdDTO) obj;
        try {
            /*connectionIgFormato = */ getConnection();
            if (sgbdDTO.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO SGBD(NOMBRE, DRIVER, URL, PUERTO, ID) VALUES(?, ?, ?, ?, ?)");
                sgbdDTO.setId(getMaximoId("SGBD"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE SGBD SET NOMBRE=?, DRIVER=?, URL=?, PUERTO=?  WHERE ID=?");
            }
            prep.setString(1, sgbdDTO.getNombre());
            prep.setString(2, sgbdDTO.getDriver());
            prep.setString(3, sgbdDTO.getUrl());
            prep.setInt(4, sgbdDTO.getPuerto().intValue());
            prep.setInt(5, sgbdDTO.getId().intValue());
            prep.executeUpdate();
            return sgbdDTO.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM SGBD WHERE ID = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex,"No se pudo eliminar el Gestor de Base de Datos.");
        } catch (Exception ex) {
            throw new IGFormatoException(ex,"No se pudo eliminar el Gestor de Base de Datos.");
        } finally {
            closeAll();
        }

    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM SGBD WHERE NOMBRE = ?");
            prep.setString(1, ((SgbdDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return Boolean.valueOf(false);
    }
}
